{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import http.client\n",
    "import traceback\n",
    "import urllib\n",
    "\n",
    "import gzip\n",
    "from io import BytesIO\n",
    "\n",
    "HTTP_OK = 200\n",
    "HTTP_AUTHORIZATION_ERROR = 401\n",
    "class Client:\n",
    "    domain = 'api.wmcloud.com'\n",
    "    port = 443\n",
    "    token = ''\n",
    "    #设置因网络连接，重连的次数\n",
    "    reconnectTimes=2\n",
    "    httpClient = None\n",
    "    def __init__( self ):\n",
    "        self.httpClient = http.client.HTTPSConnection(self.domain, self.port, timeout=60)\n",
    "    def __del__( self ):\n",
    "        if self.httpClient is not None:\n",
    "            self.httpClient.close()\n",
    "    def encodepath(self, path):\n",
    "        #转换参数的编码\n",
    "        start=0\n",
    "        n=len(path)\n",
    "        re=''\n",
    "        i=path.find('=',start)\n",
    "        while i!=-1 :\n",
    "            re+=path[start:i+1]\n",
    "            start=i+1\n",
    "            i=path.find('&',start)\n",
    "            if(i>=0):\n",
    "                for j in range(start,i):\n",
    "                    if(path[j]>'~'):\n",
    "                        re+=urllib.quote(path[j])\n",
    "                    else:\n",
    "                        re+=path[j]  \n",
    "                re+='&'\n",
    "                start=i+1\n",
    "            else:\n",
    "                for j in range(start,n):\n",
    "                    if(path[j]>'~'):\n",
    "                        re+=urllib.quote(path[j])\n",
    "                    else:\n",
    "                        re+=path[j]  \n",
    "                start=n\n",
    "            i=path.find('=',start)\n",
    "        return re\n",
    "    def init(self, token):\n",
    "        self.token=token\n",
    "    def getData(self, path):\n",
    "        result = None\n",
    "        path='/data/v1' + path\n",
    "        print (path)\n",
    "        path=self.encodepath(path)\n",
    "        for i in range(self.reconnectTimes):\n",
    "            try:\n",
    "                #set http header here\n",
    "                self.httpClient.request('GET', path, headers = {\"Authorization\": \"Bearer \" + self.token,\n",
    "                                                                \"Accept-Encoding\": \"gzip, deflate\"})\n",
    "                #make request\n",
    "                response = self.httpClient.getresponse()\n",
    "                result = response.read()\n",
    "                compressedstream = BytesIO(result)  \n",
    "                gziper = gzip.GzipFile(fileobj=compressedstream)\n",
    "                try:\n",
    "                    result = gziper.read()\n",
    "                except:\n",
    "                    pass\n",
    "                return response.status, result\n",
    "            except Exception as e:\n",
    "                if i == self.reconnectTimes-1:\n",
    "                    raise e\n",
    "                if self.httpClient is not None:\n",
    "                    self.httpClient.close()\n",
    "                self.httpClient = http.client.HTTPSConnection(self.domain, self.port, timeout=60)\n",
    "        return -1, result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/data/v1/api/HKequity/getequSHHKQuota.json?field=&exchangeCD=&tradeDate=20161027\n",
      "{\"retCode\":-403,\"retMsg\":\"Need Privilege\"}\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        client = Client()\n",
    "        client.init('f8f7e9783547b0e0f1898a67bad529c82685094bc5e946fca2b74704ee8d78b2')\n",
    "        url1='/api/HKequity/getequSHHKQuota.json?field=&exchangeCD=&tradeDate=20161027'\n",
    "        code, result = client.getData(url1)\n",
    "        if code==200:\n",
    "            print(result.decode('utf-8'))\n",
    "        else:\n",
    "            print (code)\n",
    "            print (result)\n",
    "    except Exception as e:\n",
    "        #traceback.print_exc()\n",
    "        raise e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
